package com.assignwork.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.springblade.core.tool.utils.Func;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class WorkArrangeMachines4MobileVO {

    @JsonSerialize(using = ToStringSerializer.class, nullsUsing = NullSerializer.class)
    private Long id;

    private String machines;

    private String machineType;
    private String machineTypeName;

    @JsonSerialize(using = ToStringSerializer.class, nullsUsing = NullSerializer.class)
    private Long machineId;
    private String machineName;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long teamId;
    private String teamName;

    public static boolean notNull(WorkArrangeMachines4MobileVO vo) {
        return Func.isNotBlank(vo.getMachineType()) || (vo.getMachineId() != null && vo.getMachineId() > 0);
    }

    public static WorkArrangeMachines4MobileVO toVO(WorkArrangeMachinesVO machines) {
        return new WorkArrangeMachines4MobileVO(){{
            setMachineType(machines.getMachineKind());
            setMachineName(machines.getMachineKindName());
            setTeamId(machines.getTeamId());
            setTeamName(machines.getTeamName());
            setMachines(machines.getMachines() + "");
        }};
    }

    public static WorkArrangeMachines4MobileVO toVO(WorkArrangeShipUnderMachineVO underMachine) {
        return new WorkArrangeMachines4MobileVO(){{
            setMachineType(underMachine.getMachineKind());
            setMachineTypeName(underMachine.getMachineKindName());
            setTeamId(underMachine.getMachineCorp());
            setTeamName(underMachine.getMachineCorpName());
            setMachines(underMachine.getRemark());
        }};
    }

    private static WorkArrangeMachines4MobileVO toFixed(WorkArrangeShipCabinInfoVO cabinInfo) {
        return new WorkArrangeMachines4MobileVO(){{
            setMachineId(cabinInfo.getFixedMachineId());
            setMachineName(cabinInfo.getFixedMachineName());
        }};
    }

    private static WorkArrangeMachines4MobileVO toSupport(WorkArrangeShipCabinInfoVO cabinInfo) {
        return new WorkArrangeMachines4MobileVO(){{
            setMachineType(cabinInfo.getSupportMachineKind());
            setMachineTypeName(cabinInfo.getSupportMachineKindName());
            setTeamId(cabinInfo.getSupportMachineCorp());
            setTeamName(cabinInfo.getSupportMachineCorpName());
        }};
    }

    public static List<WorkArrangeMachines4MobileVO> toVOS(WorkArrangeShipCabinInfoVO cabinInfo) {
        List<WorkArrangeMachines4MobileVO> vo = new ArrayList<>();
        vo.add(toFixed(cabinInfo));
        vo.add(toSupport(cabinInfo));
        return vo;
    }

    public static List<WorkArrangeMachines4MobileVO> data(
            List<WorkArrangeMachinesVO> machines,
            List<WorkArrangeShipCabinInfoVO> cabinInfos,
            List<WorkArrangeShipUnderMachineVO> underMachines) {

        List<WorkArrangeMachines4MobileVO> res = new ArrayList<>();
        if (Func.isNotEmpty(machines)) {
            res.addAll(machines.stream().map(WorkArrangeMachines4MobileVO::toVO).collect(Collectors.toList()));
        }

        if (Func.isNotEmpty(underMachines)) {
            res.addAll(underMachines.stream().map(WorkArrangeMachines4MobileVO::toVO).collect(Collectors.toList()));
        }

        if (Func.isNotEmpty(cabinInfos)) {
            res.addAll(cabinInfos.stream().map(WorkArrangeMachines4MobileVO::toVOS).flatMap(List::stream).collect(Collectors.toList()));
        }

        return res.stream().filter(WorkArrangeMachines4MobileVO::notNull).collect(Collectors.toList());
    }

}
